package com.siwen.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

/**
 * @author seven
 * @date 2012-3-19上午10:48:47
 */
public class ApacheZipTest {

	public static final String sep = File.separator;
	public static void main(String[] args) throws Exception{
//		System.out.println("=========System Properties========");
//		for(Map.Entry<Object, Object> entry : System.getProperties().entrySet()){
//			System.out.println(entry.getKey()+"="+entry.getValue());
//		}
//		System.out.println("=========System Properties========");
//		if(args.length < 2){
//			System.out.println("Please input arguments folder path and zip file destination!");
//			return;
//		}
		test("C:/TestZIP","C:/TestApacheZIP.zip");
//		test(args[0],args[1]);
	}
	
	public static void test(String folderPath, String zipPath) throws IOException {
		File inFile = new File(folderPath);
		ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipPath));
//		zos.setEncoding(System.getProperty("sun.jnu.encoding"));
		zos.setEncoding("GB18030");
		zos.setComment("多文件处理");
		zipFiles(inFile, zos, "");
		zos.close();
	}

	public static void zipFiles(File inFile, ZipOutputStream zos, String dir)throws IOException{
		if(inFile.isDirectory()){
			File[] files = inFile.listFiles();
			for(File f : files){
				zipFiles(f, zos, dir + sep + inFile.getName());
			}
		}else{
			String entryName = null;
			if(!"".equals(dir))
				entryName = dir + sep + inFile.getName();
			else
				entryName = inFile.getName();
			ZipEntry entry = new ZipEntry(entryName);
			zos.putNextEntry(entry);
			FileInputStream fis = new FileInputStream(inFile);
			int len = 0;
			byte[] buffer = new byte[fis.available()];
			if((len = fis.read(buffer,0,buffer.length)) > 0){
				zos.write(buffer,0,len);
			}
			zos.closeEntry();
			fis.close();
		}
	}

}
